import { TouchableOpacity } from "react-native";
import { MaterialIcons } from "@expo/vector-icons";

export default function Tips({board,setBoard,selectedCell,updateBoardHistory}){
    const getBlock=(row,col)=>{
        const blockRowStart=Math.floor(row/3)*3;
        const blockColStart=Math.floor(col/3)*3;
        let blockCells=[];
        for(let r=blockRowStart;r<blockRowStart+3;r++){
            for(let c=blockColStart;c<blockColStart+3;c++){
                blockCells.push([r,c]);
            }
        }
        return blockCells;
    }


    const handleTips=()=>{
        if(!selectedCell)return;
        const newBoard=[...board];
        const {row,col}=selectedCell;
        const blocks=getBlock(row,col);
        let usedNumbers=new Set();
        for(let i=0;i<9;i++){
            if(newBoard[row][i].value!==null) usedNumbers.add(newBoard[row][i].value);
            if(newBoard[i][col].value!==null) usedNumbers.add(newBoard[i][col].value);
        }
        blocks.forEach(([r,c])=>{
            if(newBoard[r][c].value!==null)usedNumbers.add(newBoard[r][c].value);
        })

        let numbers=[];
        for(let num=1;num<=9;num++){
            if(!usedNumbers.has(num)) numbers.push(num);
        }
        
        newBoard[row][col].noteValue=numbers;
        setBoard(newBoard);
        updateBoardHistory(newBoard);
    }
    return(
        <TouchableOpacity onPress={handleTips} disabled={!selectedCell}>
            <MaterialIcons name="tips-and-updates" size={25} color={selectedCell ? "black" : "gray"} />
        </TouchableOpacity>
    )
}